/*
 *  Copyright (c) M2TK Project. All rights reserved.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package m2tk.assistant.app.ui.dialog;

import com.formdev.flatlaf.FlatClientProperties;
import net.miginfocom.swing.MigLayout;
import org.kordamp.ikonli.fluentui.FluentUiFilledAL;
import org.kordamp.ikonli.fluentui.FluentUiFilledMZ;
import org.kordamp.ikonli.swing.FontIcon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import static m2tk.assistant.common.ui.UIConstants.LIGHT_BLUE;

public class OpenSourceDialog extends JDialog
{
    private static final Logger log = LoggerFactory.getLogger(OpenSourceDialog.class);

    private String sourceType = "";

    public OpenSourceDialog(Frame parent)
    {
        super(parent);
        setupUI();
    }

    private void setupUI()
    {
        JPanel root = new JPanel(new MigLayout("",
                                               "60[100!][100!][100!]60",
                                               "40[100!]15[30!]40"));

        JButton button1 = new JButton(FontIcon.of(FluentUiFilledAL.FOLDER_OPEN_24, 64, LIGHT_BLUE));
        JButton button2 = new JButton(FontIcon.of(FluentUiFilledAL.LIVE_24, 64, LIGHT_BLUE));
        JButton button3 = new JButton(FontIcon.of(FluentUiFilledMZ.MAP_DRIVE_24, 64, LIGHT_BLUE));
        button1.putClientProperty(FlatClientProperties.STYLE,
                                  """
                                  borderWidth: 0; focusWidth: 0; innerFocusWidth: 0; innerOutlineWidth: 0;
                                  background: null;
                                  hoverBackground: $List.selectionBackground;
                                  """);
        button2.putClientProperty(FlatClientProperties.STYLE,
                                  """
                                  borderWidth: 0; focusWidth: 0; innerFocusWidth: 0; innerOutlineWidth: 0;
                                  background: null;
                                  hoverBackground: $List.selectionBackground;
                                  """);
        button3.putClientProperty(FlatClientProperties.STYLE,
                                  """
                                  borderWidth: 0; focusWidth: 0; innerFocusWidth: 0; innerOutlineWidth: 0;
                                  background: null;
                                  hoverBackground: $List.selectionBackground;
                                  """);
        JLabel label = new JLabel();
        label.setFont(UIManager.getFont("Label.font").deriveFont(24.0f));

        MouseAdapter adapter = new MouseAdapter()
        {
            @Override
            public void mouseEntered(MouseEvent e)
            {
                JButton button = (JButton) e.getSource();
                sourceType = "";
                if (button == button1)
                    sourceType = "本地文件";
                if (button == button2)
                    sourceType = "组播流";
                if (button == button3)
                    sourceType = "第三方设备";
                label.setText(sourceType);
            }

            @Override
            public void mouseExited(MouseEvent e)
            {
                sourceType = "";
                label.setText(sourceType);
            }

            @Override
            public void mouseClicked(MouseEvent e)
            {
                closeDialog();
            }
        };
        button1.addMouseListener(adapter);
        button2.addMouseListener(adapter);
        button3.addMouseListener(adapter);

        root.add(button1, "grow");
        root.add(button2, "grow");
        root.add(button3, "grow, wrap");
        root.add(label, "span 3, align 50%");

        setContentPane(root);

        pack();
        setModal(true);
        setResizable(false);
        setTitle("选择输入源");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        getRootPane().registerKeyboardAction(e -> closeDialog(),
                                             KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
                                             JComponent.WHEN_IN_FOCUSED_WINDOW);
    }

    private void closeDialog()
    {
        setVisible(false);
        dispose();
    }

    public String selectSource()
    {
        setVisible(true);
        return sourceType;
    }
}
